#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <glade/glade.h>
#include "gtkwire_app.h"
#include "gtkwire_signal.h"
#include "gtkwire_databank.h"
#include "gtkwire_command.h"
#include "gtkwire_lock.h"

static GladeXML *gxml = NULL;

void
gtk_init_from_java (int argc, char *argv[])
{
	/* Set gtk_main lock functions. This uses java object as lock object.
	* That way we can use java synchronize to sync gtk calls */
	gdk_threads_set_lock_functions (get_java_gtk_lock, release_java_gtk_lock);

	/* Init threads */
	g_thread_init (NULL);
	gdk_threads_init ();
	
	/* Init gtk */
	gtk_init (&argc, &argv);
}

void
app_init (const gchar *glade_file)
{
	/* build GUI from Glade XML file and check */
	gxml = glade_xml_new (glade_file, NULL, NULL);
	g_assert (gxml);

	/* Connect signals using func signal_connector */	
	glade_xml_signal_autoconnect_full (gxml, signal_connector, NULL);

	/* Starts GUI main loop. This method blocks until gtk_main_quit () */
	gtk_main ();
}

void
app_init_from_window (const gchar *window_name)
{
	GtkWidget *window;

	/* Get and show main window */
	window = get_widget (window_name);
	gtk_widget_show (window);

	/* Starts GUI main loop. This method blocks until gtk_main_quit () */
	gtk_main ();
}

GtkWidget*
get_glade_widget( const char* w_name )
{
	if (gxml == NULL) return NULL;
	return glade_xml_get_widget (gxml, w_name);
}

void 
app_shutdown()
{
	/* Stops gtk_main loop */
	gtk_main_quit ();
}
